<?php
class ModelTotalCoupon extends Model {
	public function getCouponValue($data) {
		$result = 0;
		
		$total = $data['subtotal'];
		if($this->config->get('coupon_status')) {
			$coupon = $this->getCoupon($data['coupon_id']);
			
			if ($coupon) {
				$discount_total = 0;
				if((int)$coupon['discount_all']){
					$coupon_total = $total;	
				}
				else{
					if (!$coupon['product']) {
						$coupon_total = $total;
					} else {
						$coupon_total = 0;
					
						foreach ($data['products'] as $product) {
							if (in_array($product['product_id'], $coupon['product'])) {
								$coupon_total += $product['total'];
							}
						}					
					}
				}
				
				if ($coupon['type'] == 'F') {
					$coupon['discount'] = min($coupon['discount'], $coupon_total);
				}
				
				if($coupon['discount_all']){
					$discount = 0;
					
					if ($coupon['type'] == 'F') {
						$discount = $coupon['discount'];
					}
					elseif ($coupon['type'] == 'P') {
						$discount = ($coupon_total * $coupon['discount']) / 100;
					}
					
					$discount_total = $discount;
				}
				else{
					foreach ($data['products'] as $product) {
						$discount = 0;
						
						if (!$coupon['product']) {
							$status = TRUE;
						} else {
							if (in_array($product['product_id'], $coupon['product'])) {
								$status = TRUE;
							} else {
								$status = FALSE;
							}
						}
						
						if ($status) {
							if ($coupon['type'] == 'F') {
								$discount = $coupon['discount'] * ($product['total'] / $coupon_total);
							} elseif ($coupon['type'] == 'P') {
								$discount = $product['total'] / 100 * $coupon['discount'];
							}					
						}
						
						$discount_total += $discount;
					}
				}				
						
      			$result = -1 * $discount_total;
			} 
		}
		
		return $result;
	}
	
	public function getCoupon($coupon) {
		$status = TRUE;
		
		$sql = "SELECT * FROM " . DB_PREFIX . "coupon c " .
			" LEFT JOIN " . DB_PREFIX . "coupon_description cd ON (c.coupon_id = cd.coupon_id) " .
			" WHERE cd.language_id = '" . (int)$this->config->get('config_language_id') . "' " .
			"	AND c.coupon_id = '" . $this->db->escape($coupon['coupon_id']) . "' " .
			" 	AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND c.status = '1'";
		$coupon_query = $this->db->query($sql);
		
		if ($coupon_query->num_rows) {			
			$coupon_product_data = array();				
			if(!(int)$coupon_query->row['discount_all']){ 
				$coupon_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_product WHERE coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "'");
	
				foreach ($coupon_product_query->rows as $result) {
					$coupon_product_data[] = $result['product_id'];
				}
				
				if ($coupon_product_data) {
					$coupon_product = FALSE;
						
					foreach ($coupon['products'] as $product) {
						if (in_array($product['product_id'], $coupon_product_data)) {
							$coupon_product = TRUE;
								
							break;
						}
					}
						
					if (!$coupon_product) {
						$status = FALSE;
					}
				}
			}
			else{ 
				$status = TRUE;
			}
		} else {
			$status = FALSE;
		}
		
		if ($status) {
			$coupon_data = array(
				'coupon_id'     => $coupon_query->row['coupon_id'],
				'code'          => $coupon_query->row['code'],
				'name'          => $coupon_query->row['name'],
				'type'          => $coupon_query->row['type'],
				'discount'      => $coupon_query->row['discount'],
				'shipping'      => $coupon_query->row['shipping'],
				'total'         => $coupon_query->row['total'],
				'product'       => $coupon_product_data,
				'date_start'    => $coupon_query->row['date_start'],
				'date_end'      => $coupon_query->row['date_end'],
				'uses_total'    => $coupon_query->row['uses_total'],
				'uses_customer' => $coupon_query->row['uses_customer'],
				'status'        => $coupon_query->row['status'],
				'date_added'    => $coupon_query->row['date_added'],
				'discount_all'  => $coupon_query->row['discount_all']
			);
			
			return $coupon_data;
		}
	}
}
?>